package com.crape.faq.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.crape.common.exceptions.DAOException;
import com.crape.common.ibatis.SqlMapClientDaoSupportEx;
import com.crape.common.model.Answer;
import com.crape.common.model.AnswerRef;
import com.crape.faq.dao.IAnswerDAO;

@Service
public class AnswerDAOImpl extends SqlMapClientDaoSupportEx implements IAnswerDAO {

	@SuppressWarnings("unchecked")
	@Override
	public List<Answer> getPagedAnswers(Long questionid, Integer startIndex, Integer pageSize) throws DAOException {
		if (questionid == null || startIndex == null || pageSize == null || pageSize == 0) {
			return null;
		}
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("questionid", questionid);
		params.put("startIndex", startIndex);
		params.put("pageSize", pageSize);
		return (List<Answer>) this.queryList("getPagedAnswers", params);
	}
	
	@Override
	public Answer getTheAnswer(Long questionid) throws DAOException {
		if (questionid == null) {
			return null;
		}
		return (Answer) this.queryObject("getTheAnswer", questionid);
	}

	@Override
	public void insertAnswer(Long accountid, Long questionid, String answer) throws DAOException {
		if (questionid == null || answer == null || answer.isEmpty()) {
			return;
		}
		Answer params = new Answer();
		params.setAccountid(accountid);
		params.setQuestionid(questionid);
		params.setAnswer(answer);
		this.insert("insertAnswer", params);
	}
	
	@Override
	public Long countAnswers(Long questionid) throws DAOException {
		if (questionid == null) {
			return 0L;
		}
		return (Long) this.queryObject("countAnswers", questionid);
	}

	@Override
	public void deleteAnswerById(Long id) throws DAOException {
		if (id == null) {
			return;
		}
		this.insert("backupAnswerById", id);
		this.delete("deleteAnswerById", id);
	}

	@Override
	public void updateAnswer(Answer answer) throws DAOException {
		if (answer == null) {
			return;
		}
		this.update("updateAnswer", answer);
	}

	@Override
	public void insertAnswerRef(Long accountid, Long answerid, Integer type) throws DAOException {
		AnswerRef answerRef = new AnswerRef();
		answerRef.setAccountid(accountid);
		answerRef.setAnswerid(answerid);
		answerRef.setType(type);
		this.insert("insertAnswerRef", answerRef);
	}

	@Override
	public void deleteAnswerRefById(Long id) throws DAOException {
		if (id == null) {
			return;
		}
		this.insert("backupAnswerRefById", id);
		this.delete("deleteAnswerRefById", id);
	}

	@Override
	public Long countAnswerRef(Long answerid, Integer type) throws DAOException {
		if (answerid == null || type == null) {
			return 0L;
		}
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("answerid", answerid);
		params.put("type", type);
		return (Long) this.queryObject("countAnswerRef", params);
	}
	
	@Override
	public Long countAnswerRefByAccountid(Long accountid, Long answerid) throws DAOException {
		if (accountid==null||answerid == null) {
			return 0L;
		}
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("accountid", accountid);
		params.put("answerid", answerid);
		return (Long) this.queryObject("countAnswerRefByAccountid", params);
	}

}
